package com.yxq.mdt.app.modules.auction.ui;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.ToastUtils;
import com.wuhenzhizao.titlebar.widget.CommonTitleBar;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.modules.auction.model.AuctionModel;
import com.yxq.mdt.app.net.ApiModel;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 申请区县代表
 */
public class BecomeDistrictAndCountyAc extends BaseActivity {


    @BindView(R.id.titlebar)
    CommonTitleBar titlebar;
    @BindView(R.id.become_like_tame)
    TextView becomeLikeTame;
    @BindView(R.id.become_county_leader_price)
    TextView becomeCountyLeaderPrice;
    int status = -1;
    String message = null;

    @Override
    public int getLayoutId() {
        return R.layout.activity_become_district_and_county;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        questLeaderStatus(false);
    }

    /**
     * 判断条件是否允许
     *
     * @param startAc
     */
    private void questLeaderStatus(boolean startAc) {
        AuctionModel.ifBecomeArea(new ApiModel.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {

                JSONObject jsonObject = JSON.parseObject(s);
                status = jsonObject.getIntValue("state");
                if (jsonObject.containsKey("monetary")) {
                    String monetary = jsonObject.getString("monetary");
                    becomeCountyLeaderPrice.setText("您当前直推粉丝累计消费：" + monetary + "元");
                }
                if (jsonObject.containsKey("message")) {
                    message = jsonObject.getString("message");
                }
                leader(startAc, message);
            }

            @Override
            public void onError(String errMsg) {
                ToastUtils.showLong(errMsg);
            }
        });
    }

    @OnClick(R.id.become_like_tame)
    public void onViewClicked() {
        leader(true, message);
    }

    /**
     * 申请区县代理条件判断
     *
     * @param startAc 是否跳转申请页面
     * @param message 提示消息内容
     */
    private void leader(boolean startAc, String message) {
        if (status == 0) {
            //无资格申请
            becomeLikeTame.setBackgroundResource(R.drawable.shape_btn_lin_selector);
            becomeLikeTame.setTextColor(getResources().getColor(R.color.gray_pressed));
            if (startAc) {
                if (message != null) {
                    ToastUtils.showLong(message);
                } else {
                    ToastUtils.showLong("您当前不符合申请区县代理的条件");
                }
            }
            return;
        } else if (status == 1) {
            //有资格申请
            becomeLikeTame.setBackgroundResource(R.drawable.shape_btn_yellow_selector);
            becomeLikeTame.setTextColor(getResources().getColor(R.color.color_font_black));
            if (startAc) {
                //跳转申请页面
                Intent intent = new Intent(mActivity, CountyLeaderApplyActivity.class);
                startActivity(intent);
            }
        } else if (status == 2) {
            //等待审核
            becomeLikeTame.setText("等待审核中");
            becomeLikeTame.setEnabled(false);
        } else {
            //未请求状态
            questLeaderStatus(true);
        }

    }

}
